package cn.aion.dao;

import cn.aion.model.AnchorData;
import org.apache.ibatis.annotations.*;

/**
 *
 * Created by 虚空之翼 on 2016/11/24.
 */
public interface AnchorDataDAO {
    @Insert("insert into ad_anchor_data (channel_id,youku_play_total,youku_fans_total,youku_videos_total,youku_play_add,youku_fans_add,youku_videos_add,wealth,videos,last_upload,scan_time,type) " +
            "values (#{data.channelId},#{data.youkuPlayTotal},#{data.youkuFansTotal},#{data.youkuVideosTotal},#{data.youkuPlayAdd}," +
            "#{data.youkuFansAdd},#{data.youkuVideosAdd},#{data.wealth},#{data.videos},#{data.lastUpload},unix_timestamp(now()),#{data.type})")
    @Options(useGeneratedKeys = true, keyProperty = "data.id")
    int saveYouku(@Param("data") AnchorData data);

    @Insert("insert into ad_anchor_data (channel_id,iqiyi_play_total,iqiyi_fans_total,iqiyi_play_add,iqiyi_fans_add,scan_time,type) " +
            "values (#{data.channelId},#{data.iqiyiPlayTotal},#{data.iqiyiFansTotal},#{data.iqiyiPlayAdd}," +
            "#{data.iqiyiFansAdd},unix_timestamp(now()),#{data.type})")
    @Options(useGeneratedKeys = true, keyProperty = "data.id")
    int saveIqiyi(@Param("data") AnchorData data);
    @Insert("insert into ad_anchor_data (channel_id,sohu_play_total,sohu_fans_total,sohu_play_add,sohu_fans_add,scan_time,type) " +
            "values (#{data.channelId},#{data.sohuPlayTotal},#{data.sohuFansTotal},#{data.sohuPlayAdd}," +
            "#{data.sohuFansAdd},unix_timestamp(now()),#{data.type})")
    @Options(useGeneratedKeys = true, keyProperty = "data.id")
    int saveSohu(@Param("data") AnchorData data);
    @Insert("insert into ad_anchor_data (channel_id,qq_play_total,qq_fans_total,qq_play_add,qq_fans_add,scan_time,type) " +
            "values (#{data.channelId},#{data.qqPlayTotal},#{data.qqFansTotal},#{data.qqPlayAdd}," +
            "#{data.qqFansAdd},unix_timestamp(now()),#{data.type})")
    @Options(useGeneratedKeys = true, keyProperty = "data.id")
    int saveQQ(@Param("data") AnchorData data);
    @Insert("insert into ad_anchor_data (channel_id,bilibili_play_total,bilibili_fans_total,bilibili_play_add,bilibili_fans_add,scan_time,type) " +
            "values (#{data.channelId},#{data.bilibiliPlayTotal},#{data.bilibiliFansTotal},#{data.bilibiliPlayAdd}," +
            "#{data.bilibiliFansAdd},unix_timestamp(now()),#{data.type})")
    @Options(useGeneratedKeys = true, keyProperty = "data.id")
    int saveBilibili(@Param("data") AnchorData data);

    @Update("update ad_anchor_data set youku_play_total=#{data.youkuPlayTotal},youku_fans_total=#{data.youkuFansTotal},youku_videos_total=#{data.youkuVideosTotal}," +
            "youku_play_add=#{data.youkuPlayAdd},youku_fans_add=#{data.youkuFansAdd},youku_videos_add=#{data.youkuVideosAdd},wealth=#{data.wealth}," +
            "last_upload=#{data.lastUpload},scan_time=unix_timestamp(now()) where id=#{data.id}")
    int updateYouku(@Param("data") AnchorData data);
    @Update("update ad_anchor_data set iqiyi_play_total=#{data.iqiyiPlayTotal},iqiyi_fans_total=#{data.iqiyiFansTotal}," +
            "iqiyi_play_add=#{data.iqiyiPlayAdd},iqiyi_fans_add=#{data.iqiyiFansAdd}," +
            "last_upload=#{data.lastUpload},scan_time=unix_timestamp(now()) where id=#{data.id}")
    int updateIqiyi(@Param("data") AnchorData data);
    @Update("update ad_anchor_data set sohu_play_total=#{data.sohuPlayTotal},sohu_fans_total=#{data.sohuFansTotal}," +
            "sohu_play_add=#{data.sohuPlayAdd},sohu_fans_add=#{data.sohuFansAdd}," +
            "last_upload=#{data.lastUpload},scan_time=unix_timestamp(now()) where id=#{data.id}")
    int updateSohu(@Param("data") AnchorData data);
    @Update("update ad_anchor_data set qq_play_total=#{data.qqPlayTotal},qq_fans_total=#{data.qqFansTotal}," +
            "qq_play_add=#{data.qqPlayAdd},qq_fans_add=#{data.qqFansAdd}," +
            "last_upload=#{data.lastUpload},scan_time=unix_timestamp(now()) where id=#{data.id}")
    int updateQQ(@Param("data") AnchorData data);
    @Update("update ad_anchor_data set bilibili_play_total=#{data.bilibiliPlayTotal},bilibili_fans_total=#{data.bilibiliFansTotal}," +
            "bilibili_play_add=#{data.bilibiliPlayAdd},bilibili_fans_add=#{data.bilibiliFansAdd}," +
            "last_upload=#{data.lastUpload},scan_time=unix_timestamp(now()) where id=#{data.id}")
    int updateBilibili(@Param("data") AnchorData data);

    @Update("update ad_anchor_data " +
            "set play_total=youku_play_total+iqiyi_play_total+qq_play_total+sohu_play_total+bilibili_play_total," +
            "play_add=youku_play_add+iqiyi_play_add+qq_play_add+sohu_play_add+bilibili_play_add," +
            "fans_total=youku_fans_total+iqiyi_fans_total+qq_fans_total+sohu_fans_total+bilibili_fans_total," +
            "fans_add=youku_fans_add+iqiyi_fans_add+qq_fans_add+sohu_fans_add+bilibili_fans_add " +
            "where id=#{id}")
    int updateTotal(@Param("id") Long id);

    @Select("select * from ad_anchor_data where channel_id=#{channelId}")
    AnchorData selectOne(@Param("channelId") int channelId);

    // 上一次的数据
    @Select("select * from ad_anchor_data where channel_id=#{channelId} " +
            "and DATE_FORMAT(FROM_UNIXTIME(`scan_time`),'%Y-%m-%d') != DATE_FORMAT(now(),'%Y-%m-%d') order by id desc limit 1")
    AnchorData selectOnePrevious(@Param("channelId") int channelId);

    // 当天的数据
    @Select("select * from ad_anchor_data where channel_id=#{channelId} " +
            "and DATE_FORMAT(FROM_UNIXTIME(`scan_time`),'%Y-%m-%d') = DATE_FORMAT(now(),'%Y-%m-%d') order by id desc limit 1")
    AnchorData selectOneToday(@Param("channelId") int channelId);

}
